package com.hzw.saas.common.verify.config;

import com.hzw.saas.common.verify.service.IEmailService;
import com.hzw.saas.common.verify.service.ISmsService;
import com.hzw.saas.common.verify.service.impl.AliyunSmsServiceImpl;
import com.hzw.saas.common.verify.service.impl.SmtpEmailServiceImpl;
import com.hzw.saas.common.verify.service.impl.TestEmailServiceImpl;
import com.hzw.saas.common.verify.service.impl.TestSmsServiceImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 动态加载短信实现
 * 1、开发加载 TestSmsService   sms.test=true
 * 2、生产加载 AliyunSmsService
 * </p>
 *
 * @author sonam
 * @since 2021/1/29 17:06
 */
@Configuration
@RequiredArgsConstructor
@Slf4j
public class VerifyBeanConfig {

    private final SmsAliyunConfig smsAliyunConfig;
    private final EmailSmtpConfig emailSmtpConfig;

    /**
     * @return aliyunSmsService
     */
    @Bean
    @ConditionalOnProperty(name = "saas.sms.test", matchIfMissing = true, havingValue = "false")
    public ISmsService aliyunSmsService() {
        return new AliyunSmsServiceImpl(smsAliyunConfig);
    }

    /**
     * @return testSmsService
     */
    @Bean
    @ConditionalOnProperty(name = "saas.sms.test", havingValue = "true")
    public ISmsService testSmsService() {
        return new TestSmsServiceImpl();
    }

    /**
     * @return smtpEmailService
     */
    @Bean
    @ConditionalOnProperty(name = "saas.email.test", matchIfMissing = true, havingValue = "false")
    public IEmailService smtpEmailService() {
        return new SmtpEmailServiceImpl(emailSmtpConfig);
    }

    /**
     * @return testEmailService
     */
    @Bean
    @ConditionalOnProperty(name = "saas.email.test", havingValue = "true")
    public IEmailService testEmailService() {
        return new TestEmailServiceImpl();
    }


}
